package com.yunduan.management.utils;

import com.alibaba.fastjson.JSON;
import com.yunduan.management.enums.RCodeEnum;

import java.io.Serializable;

/**
 * @program: tang
 * @description: 返回json的封装。废弃原系统的 Response对象
 * @author: mr_he
 **/

public class JsonObject implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final String OK = "ok";
    private static final String ERROR = "error";
    private JsonObject.Meta meta;
    private Object data = null;

    public JsonObject(boolean issuccess) {
        this.meta = new Meta(issuccess, issuccess ? OK : ERROR);
    }

    public JsonObject() {
    }


    public JsonObject success() {
        this.meta = new JsonObject.Meta(true, OK);
        return this;
    }

    public JsonObject success(Object data) {
        this.meta = new JsonObject.Meta(true, OK);
        this.data = data;
        return this;
    }

    public JsonObject success(Object data, String message) {
        this.meta = new JsonObject.Meta(true, OK);
        this.meta.message = message;
        this.data = data;
        return this;
    }

    public JsonObject failure(String message) {
        this.meta = new Meta(false, message);
        return this;
    }

    public JsonObject failure(RCodeEnum code, String message) {
        this.meta = new Meta(false, code, message);
        return this;
    }

    public JsonObject failure(String code, String message) {
        this.meta = new Meta(false, code, message);
        return this;
    }

    //    废弃的方法，返回的json无法做格式化处理
    @Deprecated
    public Object toJson() {
        return JSON.toJSON(this);
    }

    public class Meta {

        private boolean success;
        private String message;
        private String code;
        // private String token;

        public Meta(boolean success) {
            this.code = success ? RCodeEnum.SUCCESS.getCode() : RCodeEnum.FAIL.getCode();
            this.success = success;
        }

        public Meta(boolean success, String message) {
            this.code = success ? RCodeEnum.SUCCESS.getCode() : RCodeEnum.FAIL.getCode();
            this.success = success;
            this.message = message;
        }

        public Meta(boolean success, RCodeEnum code, String message) {
            this.code = code.getCode();
            this.success = success;
            this.message = message;
        }

        public Meta(boolean success, String code, String message) {
            this.success = success;
            this.message = message;
            this.code = code;
        }

        public boolean isSuccess() {
            return success;
        }

        public String getMessage() {
            return message;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public static String getOK() {
        return OK;
    }

    public static String getERROR() {
        return ERROR;
    }

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
